# coding=utf-8
# -*- coding: UTF-8 -*-
#set FLASK_DEBUG=1
#set FLASK_ENV=development
import time

from flask import Flask, Blueprint
from flask import (request,make_response,stream_with_context,Response)
from markupsafe import escape


bp = Blueprint('demo', __name__, url_prefix='/')

@bp.route("/hello")
def hello_world():
    print(request)
    response = make_response( f"中文测试--in demo,Hello, World! request: {request}")
    response.headers['Content-Type'] = 'text/plain;charset=UTF-8'
    return response


@bp.route("/user/<id>")
def get_user(id):
    response = make_response( f"测试测试test getUser 3 -- {id} ; request [{request}]")
    response.headers['Content-Type'] = 'text/plain'
    return response
@bp.route("/chat/<msg>")
def chat(msg):
    response = make_response( f"你发送的是 -- {msg}  ")
    response.headers['Content-Type'] = 'text/plain;charset=UTF-8'
    return response

@bp.route("/stream/<msg>")
def streamdemo(msg=None):
    if msg is None:
        msg = "中文测试"
    def generate():
        yield '<p>Hello '
        for n in range(0,10):
            yield escape(f"test\ner {msg} {n}\n")
            yield '<br/>'
            time.sleep(0.5)
        yield '!</p>'

    response = Response(stream_with_context(generate()))
    response.headers["Access-Control-Allow-Origin"]="*"
    return response